﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BUS;
using DAL;
namespace QuanLyNhaSach
{
    public partial class Form_QLTacGia : DevComponents.DotNetBar.Office2007Form
    {
        Form_Them_CapNhat_TacGia frm;
        TacGia_BUS tacgia_bus;
        TacGia_DTO tacgia_dto;

        public Form_QLTacGia()
        {
            InitializeComponent();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frm = new Form_Them_CapNhat_TacGia();
            frm.KhiThemMoi += frm_KhiThemMoi;
            frm.Action = "Insert";
            frm.ShowDialog();
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            frm = new Form_Them_CapNhat_TacGia();
            frm.KhiCapNhat += frm_KhiCapNhat;
            frm.Action = "Update";
            frm.ID = dtg_QLTacGia.Rows[dong].Cells["Str_MaTacGia"].Value.ToString();
            frm.ShowDialog();
        }

        private void Form_QLTacGia_Load(object sender, EventArgs e)
        {
            dtg_QLTacGia.AutoGenerateColumns = false;
            LoadDanhSachTacGia();
        }
        void LoadDanhSachTacGia()
        {
            tacgia_bus = new TacGia_BUS();
            dtg_QLTacGia.DataSource = tacgia_bus.LayDanhSachTacGia();
        }
        void frm_KhiThemMoi()
        {
            LoadDanhSachTacGia();
        }
        void frm_KhiCapNhat()
        {
            LoadDanhSachTacGia();
        }
        int dong;
        private void dtg_QLTacGia_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
        }

        List<string> MaTacGia;
        
        private void btnXoa_Click(object sender, EventArgs e)
        {
            MaTacGia = new List<string>();
            for (int i = 0; i < dtg_QLTacGia.Rows.Count; i++)
            {
                if (dtg_QLTacGia.Rows[i].Cells["CHECKBOX"].Value != null)
                    MaTacGia.Add(dtg_QLTacGia.Rows[i].Cells["Str_MaTacGia"].Value.ToString());
            }
            if (MaTacGia.Count == 0)
            {
                MessageBoxEx.Show("Bạn chưa chọn tác giả để xóa.", "Thông báo");
                return;
            }

            if (MessageBoxEx.Show("Bạn có chắc chắn xóa dòng này không?", "Xóa tác giả", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                for (int i = 0; i < MaTacGia.Count; i++)
                {
                    tacgia_dto = new TacGia_DTO();
                    tacgia_bus = new TacGia_BUS();
                    tacgia_dto.Str_MaTacGia = MaTacGia[i].ToString();
                    tacgia_bus.XoaTacGia(tacgia_dto);
                }
                LoadDanhSachTacGia();
                MaTacGia.Clear();
            }
            else
            {
                for (int i = 0; i < dtg_QLTacGia.Rows.Count; i++)
                {
                    dtg_QLTacGia.Rows[i].Cells["CHECKBOX"].Value = false;
                }
            }
        }

        private void dtg_QLTacGia_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            btnCapNhat_Click(sender, e);
        }

        private void dtg_QLTacGia_Click(object sender, EventArgs e)
        {
            if (dtg_QLTacGia.Rows[dong].Cells["CHECKBOX"].Value == null)
                dtg_QLTacGia.Rows[dong].Cells["CHECKBOX"].Value = true;
            else
                dtg_QLTacGia.Rows[dong].Cells["CHECKBOX"].Value = null;
        }
    }
}
